var express = require('express')
var app = express()
var cors = require('cors')
var app = express()

app.use(express.json())
app.use(
  express.urlencoded({
    extended: true,
  })
)

app.use(cors())

// 登录接口
app.post('/api/login', function (req, res) {
  let { username, password } = req.body
  if (username === 'admin') {
    res.json({
      code: 20000,
      token: 'admin-token',
    })
  } else {
    res.json({
      code: 20000,
      token: 'normal-token',
    })
  }
})

// 获取用户信息
app.get('/api/userinfo', function (req, res) {
  let { token } = req.query
  if (!token) {
    res.json({
      code: 20001,
      msg: 'token失效',
    })
    return
  }
  if (token === 'admin-token') {
    // 超级用户
    let response = {
      code: 20000,
      data: {
        id: 1,
        // 菜单权限（和前端路由表中的name字段一致）
        routes: [
          'goodsmanage',
          'goodslist',
          'goodsadd',
          'ordermanage',
          'orderlist',
        ],
        // 按钮权限
        btns: ['goods:list:add', 'goods:list:edit', 'goods:list:detail'],
        name: '超级用户',
        avatar:
          'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      },
    }
    res.json(response)
  } else {
    // 普通用户
    let response = {
      code: 20000,
      data: {
        id: 2,
        // 菜单权限
        routes: ['goodsmanage', 'goodslist'],
        // 按钮权限
        btns: ['goods:list:detail'],
        name: '普通用户',
        avatar:
          'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      },
    }
    res.json(response)
  }
})

app.listen(4000, () => {
  console.log('server is running at port 4000')
})
